#include <stdio.h>

int strlen(const char *str)
{
    int len=0;
    while(*str++!='\0')
        len++;
    return len;
}

char *strcpy(char *dest, const char *src)
{
        char *old = dest;
        while(*dest++ = *src++);
        return old;
}

char *strncpy(char *dest, const char *src, int n)
{
    char *old=dest;
    while(n>0 && (*dest++=*src++))
        n--;
    return old;
}

int strcmp(const char *str1, const char *str2)
{
    while(*str1)
    {
        if(*str1!=*str2)
            return *str1>*str2 ? 1 : -1;
        *str1++;
        *str2++;
    }
    return 0;
}

int strncmp(const char *str1, const char *str2, int n)
{
    while(*str1 && n>0)
    {
        if(*str1!=*str2)
            return *str1>*str2 ? 1 : -1;
        *str1++;
        *str2++;
        n--;
    }
    return 0;
}

char *strcat(char* dest, const char *src)
{
    char *old=dest;
    while(*dest)
        *dest++;
    while(*dest++=*src++);
    return old;
}

char *strncat(char *dest, const char* src, int n)
{
    char* old=dest;
    while(*dest)
        *dest++;
    while(n>0 && (*dest++=*src++))
    n--;
    return old;
}

char *strchr (const char *str, int c)
{
    while(*str++)
        if(*str==c) return (char*)str;
    return NULL;
}
char *strrchr(const char *str, int c)
{
     const char *last = NULL;
     if (c == '\0')
         return strchr(str, c);
     while ((str = strchr(str, c)) != NULL)
     {
         last = str;
         str++;
     }
     return (char*)last;
}
int main()
{
        char s1[] = "Hi there, fred!";
        char s2[] = "Hi thzre, fred!";
        printf("%i\n",strlen(s1));
        printf("%i\n",strncmp(s1,s2,6));
        printf("%x\n", strchr(s1,'e'));
        printf("%x\n", strrchr(s1,'e'));
        return 0;
}
